package greedy_algorithm.nowcoder.medium;

/**
 * @author bruin_du
 * @description 手套
 * https://www.nowcoder.com/practice/365d5722fff640a0b6684391153e58d8?
 * tpId=49&&tqId=29337&rp=1&ru=/activity/oj&qru=/ta/2016test/question-ranking
 * @date 2022/5/15 23:28
 **/
public class Gloves {
    public int findMinimum(int n, int[] left, int[] right) {
        int sumL = 0,sumR = 0,sum = 0;
        int minL = Integer.MAX_VALUE,minR = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            if(left[i] * right[i] == 0)
                sum += left[i] + right[i];
            else{
                if(left[i] < minL)
                    minL = left[i];
                if(right[i] < minR)
                    minR = right[i];
                sumL += left[i];
                sumR += right[i];
            }
        }
        if(sumL > sumR){
            sum += sumR - minR + 2;
        }else{
            sum += sumL - minL + 2;
        }
        return sum;
    }
}
